import time
from greenlet import greenlet


class GreenletDemo:
    def __init__(self):
        self.gr1 = greenlet(self.work1)
        self.gr2 = greenlet(self.work2)

    def work1(self):
        while True:
            print('---work1---')
            self.gr2.switch()
            time.sleep(0.2)

    def work2(self):
        while True:
            print('---work2---')
            self.gr1.switch()
            time.sleep(0.2)

    def _test_greenlet(self):
        """
        gd = GreenletDemo()
        gd._test_greenlet()
        """
        self.gr1.switch()


if __name__ == '__main__':
    gd = GreenletDemo()
    gd._test_greenlet()
